/**
 * 
 */
package com.smarthome.core.area.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.smarthome.base.service.ShAreaProvinceService;
import com.smarthome.core.area.AreaCityService;
import com.smarthome.core.area.AreaProvinceService;
import com.smarthome.domain.bean.ShAreaCity;
import com.smarthome.domain.bean.ShAreaProvince;

/**
 * @author dhc
 * @date 2015年10月12日 下午6:00:50
 * @title
 */
@Service("areaProvinceService")
public class AreaProvinceServiceImpl implements AreaProvinceService {

	@Resource(name="shAreaProvinceService")
	private ShAreaProvinceService areaProvinceService;
	@Resource(name="areaCityService")
	private AreaCityService areaCityService;
	
	
	@Override
	public ShAreaProvince queryAreaProvinceById(Long id) {
		ShAreaProvince province = areaProvinceService.selectEntryById(id);
		return province;
	}

	@Override
	public List<ShAreaProvince> queryShAreaProvinces(ShAreaProvince areaProvince) {
		List<ShAreaProvince> provinces = areaProvinceService.selectEntryList(areaProvince);
		return provinces;
	}
	
	
	public void addParam(List<ShAreaProvince> provinces){
		if(null == provinces){
			return ;
		}
		
		List<ShAreaCity> citys = areaCityService.queryShAreaCitys(null);
		
		if(null != citys && citys.size() > 0){
			Map<Long,List<ShAreaCity>> map =new HashMap<Long,List<ShAreaCity>>();
			
			for(ShAreaCity city : citys){
				List<ShAreaCity> c = map.get(city.getProvinceId());
				if(c == null){
					c = new ArrayList<ShAreaCity>();
					map.put(city.getProvinceId(),  c);
				}
				c.add(city);
			}
			areaCityService.addParam(citys);
			for(ShAreaProvince p : provinces){
				p.setCitys(map.get(p.getId()));
			}
		}
		
	}
	

}
